/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vtc.app;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

/**
 *
 * @author Admin
 */
public class GameCanvasProcess extends GameCanvasBase {

    private Image imgTileset, imgBullet, imgMario, imgExplosion, imgEnemyNam, imgBoss, imgEnemyRuaDo, imgEnemyRuaBay, imgEnemyMatXanh;
    private Image imgRuaMa, imgBanh, imgCay, imgCongChua, imgGate, imgBoss2, imgWin;
    private LayerManager lm;
    private TiledLayer road, backGround, layout, gameOver;
    private MarioSpite mario;
    private Sprite gate;
    private BulletSprite bullet;
    private String hdir = "l", vdir = "d";
    private int tx, ty;
    private int jump;
    private int mapW;
    private int mapH;
    private Vector vtrBullet = new Vector();
    private Vector vtrExplosion = new Vector();
    private Vector vtrBrick = new Vector();
    private Vector vtrEnemy = new Vector();
    private Vector vtrEnemyRuaSat = new Vector();
    private Vector vtrEnemyRuaMa = new Vector();
    private Vector vtrEnemyCay = new Vector();
    private Vector vtrEnemyDan = new Vector();
    private Vector vtrEnemyMatXanh = new Vector();
    private Vector vtrEnemyLuaMa = new Vector();
    private Vector vtrBoss = new Vector();
    private Vector vtrBoss2 = new Vector();
    private Vector vtrDauHoi = new Vector();
    private Vector vtrBanh = new Vector();
    private Vector vtrCongChua = new Vector();
    private int bossHp;
    private boolean marioAlive = true;
    private boolean win = true;
    private int fallDown = 3;
    private int level = 1;

    public GameCanvasProcess(boolean suppressKeyEvents) {
        super(suppressKeyEvents);
    }

    public void runGame() {

        tx = mario.getX();
        ty = mario.getY();

        this.banhProcess();
        this.dauHoiProcess();
        this.enemyProcess();
        this.checkCollidesBrick();
        this.checkKey(this.getKeyStates());
        this.checkCollidesRoad();
        this.viewportProcess();
        this.explosionProcess();
        try {
            this.bulletProcess();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        this.render(this.getGraphics());
        flushGraphics();
    }

    public void render(Graphics g) {
        if (win) {
            if (marioAlive) {
                g.setColor(255, 255, 255);
                g.fillRect(0, 0, w, h);
                g.setClip(0, 0, w, h);
                this.lm.paint(g, 0, 0);
            } else {
                g.setColor(0, 0, 0);
                g.fillRect(0, 0, w, h);
                g.setColor(255, 255, 255);
                g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE));
                g.drawString("Game Over", w / 2, h / 2, g.BASELINE | g.HCENTER);
            }
        } else {
            g.setColor(0, 0, 0);
            g.fillRect(0, 0, w, h);
            g.setColor(255, 255, 255);
            g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE));
            g.drawString("CONGRATULATION !", w / 2, h / 2, g.BASELINE | g.HCENTER);
            g.drawString("YOU ARE WINNER", w / 2, h / 2 + 20, g.BASELINE | g.HCENTER);
            g.drawImage(imgWin, w / 2, h / 2 - 50, g.HCENTER | g.VCENTER);
        }
    }
// dieu khien mario

    public void checkKey(int key) {
        if ((key & LEFT_PRESSED) != 0) {
            if (!hdir.equals("l")) {
                mario.setFrameSequence(new int[]{0, 1, 2, 3, 4, 5, 6, 7});
            }
            mario.setPosition(mario.getX() - mario.getSpeed(), mario.getY());
            mario.nextFrame();
            hdir = "l";
        } else if ((key & RIGHT_PRESSED) != 0) {
            if (!hdir.equals("r")) {
                mario.setFrameSequence(new int[]{8, 9, 10, 11, 12, 13, 14, 15});
            }
            mario.setPosition(mario.getX() + mario.getSpeed(), mario.getY());
            mario.nextFrame();
            hdir = "r";
        }
        if ((key & UP_PRESSED) != 0 && jump == 0 && !hdir.equals("d")) {
//            this.playSound("/com/vtc/sound/Jump.wav");
            jump = 21;
            System.out.println("toa do mario X = " + mario.getX() + "toa do mario Y = " + mario.getY());
            System.out.println("hdir u " + vdir);
        }
        if (jump > 10) {
            hdir = "u";
            vdir = "u";
            mario.setPosition(mario.getX(), mario.getY() - fallDown - 1);
            mario.setPosition(mario.getX(), mario.getY() - fallDown - 1);
            jump--;
        }
        // mario cham man hinh thi dung lai
        if (mario.getX() < 0) {
            mario.setPosition(0, mario.getY());
        }
        // ban dan
        if ((key & FIRE_PRESSED) != 0) {
            if (this.hdir.equals("l")) {
                bullet.setDir(BaseSprite.LEFT);
            }
            if (this.hdir.equals("r")) {
                bullet.setDir(BaseSprite.RIGHT);
            }
            bullet.setPosition(tx + 5, ty + 16);
            this.vtrBullet.addElement(bullet);
            this.lm.insert(bullet, 0);
        }
        // kiem tra va cham voi tuong gach
        for (int i = 0; i < vtrBrick.size(); i++) {
            BrickSprite bs = (BrickSprite) vtrBrick.elementAt(i);
            if (mario.collidesWith(bs, true)) {
                if (this.hdir.equals("r") | this.hdir.equals("l")) {
                    mario.setPosition(tx, ty);
                }
            }
        }
        // kiem tra va cham voi dau hoi
        for (int i = 0; i < vtrDauHoi.size(); i++) {
            BrickSprite dh = (BrickSprite) vtrDauHoi.elementAt(i);
            if (mario.collidesWith(dh, true) && (hdir.equals("l") | hdir.equals("r"))) {
                mario.setPosition(tx, ty);
            }
        }
        // kiem tra va cham voi nen den
        if (mario.collidesWith(gameOver, true)) {
            marioAlive = false;

        }
        if (mario.collidesWith(road, true) && (hdir.equals("l") | hdir.equals("r"))) {
            mario.setPosition(tx, ty);
        }

    }
// kiem tra va cham voi con duong

    public void checkCollidesRoad() {
        if (mario.collidesWith(road, true)) {
            mario.setPosition(tx, ty);
        } else {
            mario.setPosition(mario.getX(), mario.getY() + fallDown);
            vdir = "d";
            while (mario.collidesWith(road, true)) {
                mario.setPosition(mario.getX(), mario.getY() - fallDown);
                jump = 0;
            }
        }
    }

// kiem tra va cham voi tuong gach theo chieu len xuong
    public void checkCollidesBrick() {
        for (int i = 0; i < vtrBrick.size(); i++) {
            BrickSprite bs = (BrickSprite) vtrBrick.elementAt(i);
            if (mario.collidesWith(bs, true)) {
                if (this.vdir.equals("d")) {
                    mario.setPosition(mario.getX(), mario.getY() - fallDown);
                    jump = 0;
                }
            }
            if (mario.collidesWith(bs, true)) {
                if (hdir.equals("u")) {
                    hdir = "d";
                    Sprite fire = new Sprite(imgExplosion, 64, 64);
                    fire.setPosition(mario.getX() - 28, mario.getY() - 28);
                    this.vtrExplosion.addElement(fire);
                    this.lm.insert(fire, 0);
                    vtrBrick.removeElement(bs);
                    lm.remove(bs);

                }
            }
        }
    }

    public void khaiBaoBrick() {

        BrickSprite br1 = new BrickSprite(imgTileset, 16, 16, 0);
        br1.setFrame(55);
        br1.setPosition(110, 100);
        this.vtrBrick.addElement(br1);
        this.lm.insert(br1, 0);

        BrickSprite br2 = new BrickSprite(imgTileset, 16, 16, 0);
        br2.setFrame(55);
        br2.setPosition(142, 100);
        this.vtrBrick.addElement(br2);
        this.lm.insert(br2, 0);

        BrickSprite br4 = new BrickSprite(imgTileset, 16, 16, 0);
        br4.setFrame(55);
        br4.setPosition(535, 190);
        this.vtrBrick.addElement(br4);
        this.lm.insert(br4, 0);

        BrickSprite br11 = new BrickSprite(imgTileset, 16, 16, 0);
        br11.setFrame(55);
        br11.setPosition(503, 190);
        this.vtrBrick.addElement(br11);
        this.lm.insert(br11, 0);

        BrickSprite br8 = new BrickSprite(imgTileset, 16, 16, 0);
        br8.setFrame(55);
        br8.setPosition(350, 150);
        this.vtrBrick.addElement(br8);
        this.lm.insert(br8, 0);

        BrickSprite br10 = new BrickSprite(imgTileset, 16, 16, 0);
        br10.setFrame(55);
        br10.setPosition(382, 150);
        this.vtrBrick.addElement(br10);
        this.lm.insert(br10, 0);

    }
// su ly vien dan

    public void bulletProcess() throws IOException {
        for (int i = 0; i < vtrBullet.size(); i++) {
            BulletSprite b = (BulletSprite) this.vtrBullet.elementAt(i);
            b.moveSprite();
            b.nextFrame();
//dan ra khoi khung hinh thi xoa
            if ((b.getX() <= 0) | (b.getX() >= mapW) | (b.getY() <= 0) | (b.getY() >= mapH)) {
                this.vtrBullet.removeElement(b);
                this.lm.remove(b);
            }
// kiem tra va cham cua dan va khoi chan
            if (b.collidesWith(road, true)) {
                this.vtrBullet.removeElement(b);
                this.lm.remove(b);
                Sprite fire = new Sprite(imgExplosion, 64, 64);
                fire.setPosition(b.getX() - 28, b.getY() - 28);
                this.vtrExplosion.addElement(fire);
                this.lm.insert(fire, 0);
            }
// kiem tra va cham cua dan va enemy nam, rua do, rua bay
            for (int j = 0; j < vtrEnemy.size(); j++) {
                EnemySprite es = (EnemySprite) vtrEnemy.elementAt(j);
                if (b.collidesWith(es, true)) {
                    vtrEnemy.removeElement(es);
                    vtrBullet.removeElement(b);
                    this.lm.remove(b);
                    this.lm.remove(es);
                    Sprite fire = new Sprite(imgExplosion, 64, 64);
                    fire.setPosition(b.getX() - 28, b.getY() - 28);
                    this.vtrExplosion.addElement(fire);
                    this.lm.insert(fire, 0);
                }
            }
// kiem tra va cham cua dan va enemy rua sat
            for (int j = 0; j < vtrEnemyRuaSat.size(); j++) {
                EnemySprite es = (EnemySprite) vtrEnemyRuaSat.elementAt(j);
                if (b.collidesWith(es, true)) {
                    vtrBullet.removeElement(b);
                    this.lm.remove(b);
                    Sprite fire = new Sprite(imgExplosion, 64, 64);
                    fire.setPosition(b.getX() - 28, b.getY() - 28);
                    this.vtrExplosion.addElement(fire);
                    this.lm.insert(fire, 0);
                }
            }
// kiem tra va cham cua dan va tuong gach
            for (int j = 0; j < vtrBrick.size(); j++) {
                BrickSprite br = (BrickSprite) vtrBrick.elementAt(j);
                if (b.collidesWith(br, true)) {
                    vtrBullet.removeElement(b);
                    this.lm.remove(b);
                    Sprite fire = new Sprite(imgExplosion, 64, 64);
                    fire.setPosition(b.getX() - 28, b.getY() - 28);
                    this.vtrExplosion.addElement(fire);
                    this.lm.insert(fire, 0);
                }
            }
// kiem tra va cham cua vien dan va cay
            for (int j = 0; j < vtrEnemyCay.size(); j++) {
                EnemySprite cay = (EnemySprite) vtrEnemyCay.elementAt(j);
                if (b.collidesWith(cay, true)) {
                    vtrBullet.removeElement(b);
                    this.lm.remove(b);
                    vtrEnemyCay.removeElement(cay);
                    this.lm.remove(cay);
                    Sprite fire = new Sprite(imgExplosion, 64, 64);
                    fire.setPosition(b.getX() - 28, b.getY() - 28);
                    this.vtrExplosion.addElement(fire);
                    this.lm.insert(fire, 0);
                }
            }
// kiem tra va cham cua vien dan va boss LV1
            for (int j = 0; j < vtrBoss.size(); j++) {
                BossSprite bs = (BossSprite) vtrBoss.elementAt(j);

                if (b.collidesWith(bs, true)) {
                    vtrBullet.removeElement(b);
                    this.lm.remove(b);
                    bossHp--;
                    Sprite fire = new Sprite(imgExplosion, 64, 64);
                    fire.setPosition(b.getX() - 28, b.getY() - 28);
                    this.vtrExplosion.addElement(fire);
                    this.lm.insert(fire, 0);
                    if (bossHp == 0) {
                        vtrBoss.removeElement(bs);
                        this.lm.remove(bs);
                        level = level + 1;
                        vtrBrick.removeAllElements();
                        vtrEnemy.removeAllElements();
                        vtrEnemyRuaSat.removeAllElements();
                        vtrEnemyCay.removeAllElements();
                        vtrDauHoi.removeAllElements();
                        this.loadGame();
                    }
                }
            }
// kiem tra va cham cua vien dan va boss LV2
            for (int j = 0; j < vtrBoss2.size(); j++) {
                BossSprite bs = (BossSprite) vtrBoss2.elementAt(j);

                if (b.collidesWith(bs, true)) {
                    vtrBullet.removeElement(b);
                    this.lm.remove(b);
                    bossHp--;
                    Sprite fire = new Sprite(imgExplosion, 64, 64);
                    fire.setPosition(b.getX() - 28, b.getY() - 28);
                    this.vtrExplosion.addElement(fire);
                    this.lm.insert(fire, 0);

                    if (bossHp == 0) {
                        vtrBoss2.removeElement(bs);
                        this.lm.remove(bs);
                        Sprite fire1 = new Sprite(imgExplosion, 64, 64);
                        fire1.setPosition(gate.getX(), gate.getY());
                        this.vtrExplosion.addElement(fire1);
                        this.lm.insert(fire1, 0);
                        this.lm.remove(gate);

                        Sprite fire2 = new Sprite(imgExplosion, 64, 64);
                        fire2.setPosition(gate.getX() + 32, gate.getY());
                        this.vtrExplosion.addElement(fire2);
                        this.lm.insert(fire2, 0);

                        Sprite fire3 = new Sprite(imgExplosion, 64, 64);
                        fire3.setPosition(gate.getX() + 16, gate.getY() + 32);
                        this.vtrExplosion.addElement(fire3);
                        this.lm.insert(fire3, 0);

                    }
                }
            }
        }
    }
// xu ly enemy

    public void enemyProcess() {
// xu ly nhom enemy nam, rua do, rua bay        
        for (int i = 0; i < vtrEnemy.size(); i++) {
            EnemySprite es = (EnemySprite) vtrEnemy.elementAt(i);
            es.nextFrame();
            es.moveSprite();
            if (es.collidesWith(road, true)) {
                int dir = es.getDir();
                if (dir == 1) {
                    es.setDir(EnemySprite.RIGHT);
                    es.setFrameSequence(new int[]{2, 3});
                }
                if (dir == 2) {
                    es.setDir(EnemySprite.LEFT);
                    es.setFrameSequence(new int[]{0, 1});
                }
            }
            if (es.getX() < 0) {
                es.setDir(EnemySprite.RIGHT);
                es.setFrameSequence(new int[]{2, 3});
            }
            if (es.getX() > mapW) {
                es.setDir(EnemySprite.LEFT);
                es.setFrameSequence(new int[]{0, 1});
            }
            if (mario.collidesWith(es, true)) {
                if (mario.getY() < es.getY() - 25) {
                    mario.setPosition(tx, ty - 10);
                    es.setFrameSequence(new int[]{4});
                    vtrEnemy.removeElement(es);
                    es.die();
                } else {
                    marioAlive = false;
                }
            }
        }

//xu ly nhom enemy rua sat        
        for (int i = 0; i < vtrEnemyRuaSat.size(); i++) {
            EnemySprite ruaSat = (EnemySprite) vtrEnemyRuaSat.elementAt(i);
            ruaSat.nextFrame();
            ruaSat.moveSprite();
            if (ruaSat.collidesWith(road, true)) {
                int dir = ruaSat.getDir();
                if (dir == 1) {
                    ruaSat.setDir(EnemySprite.RIGHT);
                    ruaSat.setFrameSequence(new int[]{5, 6});
                }
                if (dir == 2) {
                    ruaSat.setDir(EnemySprite.LEFT);
                    ruaSat.setFrameSequence(new int[]{7, 8});
                }
            }
            if (ruaSat.getX() < 0) {
                ruaSat.setDir(EnemySprite.RIGHT);
                ruaSat.setFrameSequence(new int[]{5, 6});
            }
            if (ruaSat.getX() > mapW) {
                ruaSat.setDir(EnemySprite.LEFT);
                ruaSat.setFrameSequence(new int[]{7, 8});
            }
            if (mario.collidesWith(ruaSat, true)) {
                if (mario.getY() < ruaSat.getY() - 15) {
                    mario.setPosition(tx, ty - 10);
                    ruaSat.setFrameSequence(new int[]{9});
                    vtrEnemyRuaSat.removeElement(ruaSat);
                    ruaSat.die();
                } else {
                    marioAlive = false;
                }
            }
        }
//xu ly boss lv1
        for (int i = 0; i < vtrBoss.size(); i++) {
            BossSprite boss = (BossSprite) vtrBoss.elementAt(i);
            boss.nextFrame();
            boss.moveSprite();
            if (boss.collidesWith(road, true)) {
                if (boss.getDir() == BossSprite.RIGHT) {
                    boss.setDir(BossSprite.LEFT);
                } else if (boss.getDir() == BossSprite.LEFT) {
                    boss.setDir(BossSprite.RIGHT);
                }
            }
            if (boss.getX() < 0) {
                boss.setDir(EnemySprite.RIGHT);
            }
            if (boss.getX() > mapW) {
                boss.setDir(EnemySprite.LEFT);
            }
            if (mario.collidesWith(boss, true)) {
                marioAlive = false;
            }
        }
//xu ly enemy cay
        for (int i = 0; i < vtrEnemyCay.size(); i++) {
            EnemySprite cay = (EnemySprite) vtrEnemyCay.elementAt(i);
            cay.nextFrame();
            cay.moveSprite();
            if (cay.getY() < cay.gocY + 2) {
                cay.setDir(EnemySprite.DOWN);
            }
            if (cay.getY() > cay.gocY + cay.getHeight() + 10) {
                cay.setDir(EnemySprite.UP);
            }
            if (mario.collidesWith(cay, true)) {
                marioAlive = false;
            }
        }
//xu ly vien dan den
        for (int i = 0; i < vtrEnemyDan.size(); i++) {
            EnemySprite dan = (EnemySprite) vtrEnemyDan.elementAt(i);
            dan.moveSprite();
            if (dan.collidesWith(road, true)) {
                dan.setPosition(dan.gocX, dan.gocY);
            }
            if (mario.collidesWith(dan, true)) {
                if (mario.getY() < dan.getY() - 15) {
                    mario.setPosition(tx, ty - 10);
                    vtrEnemyDan.removeElement(dan);
                    lm.remove(dan);
                } else {
                    marioAlive = false;
                }
            }
        }
//xu ly enemy mat xanh        
        for (int i = 0; i < vtrEnemyMatXanh.size(); i++) {
            EnemySprite matXanh = (EnemySprite) vtrEnemyMatXanh.elementAt(i);
            matXanh.moveSprite();
            if (matXanh.getY() > 250) {
                matXanh.setDir(EnemySprite.UP);
            }
            if (matXanh.getY() < 33) {
                matXanh.setDir(EnemySprite.DOWN);
            }
            if (matXanh.collidesWith(road, true)) {
                if (matXanh.getDir() == EnemySprite.UP) {
                    matXanh.setDir(EnemySprite.DOWN);
                }
                if (matXanh.getDir() == EnemySprite.DOWN) {
                    matXanh.setDir(EnemySprite.UP);
                }
            }
            if (mario.collidesWith(matXanh, true)) {
                marioAlive = false;
            }
        }

//xu ly nhom enemy lua ma
        for (int i = 0; i < vtrEnemyLuaMa.size(); i++) {
            EnemySprite luaMa = (EnemySprite) vtrEnemyLuaMa.elementAt(i);
            luaMa.nextFrame();
            luaMa.moveSprite();
            if (luaMa.getY() < 150) {
                luaMa.setDir(EnemySprite.DOWN);
                luaMa.setFrameSequence(new int[]{14, 15});
            }
            if (luaMa.getY() > 400) {
                luaMa.setDir(EnemySprite.UP);
                luaMa.setFrameSequence(new int[]{12, 13});
            }
            if (mario.collidesWith(luaMa, true)) {
                marioAlive = false;
            }
        }

//xu ly cong chua
        for (int i = 0; i < vtrCongChua.size(); i++) {
            PrincessSprite pr = (PrincessSprite) vtrCongChua.elementAt(i);
            pr.moveSprite();
            pr.nextFrame();
            if (pr.collidesWith(gate, true)) {
                pr.setDir(PrincessSprite.LEFT);
                pr.setFrameSequence(new int[]{1, 2});
            }
            if (pr.collidesWith(road, true)) {
                pr.setDir(PrincessSprite.RIGHT);
                pr.setFrameSequence(new int[]{3, 4});
            }
            if (mario.collidesWith(pr, true)) {
                win = false;
            }
        }
//xu ly boss lv2
        for (int i = 0; i < vtrBoss2.size(); i++) {
            BossSprite boss = (BossSprite) vtrBoss2.elementAt(i);
            boss.nextFrame();
            boss.moveSprite();
            if (boss.collidesWith(road, true)) {
                if (boss.getDir() == BossSprite.RIGHT) {
                    boss.setDir(BossSprite.LEFT);
                    boss.setFrameSequence(new int[]{9, 10, 11, 12, 13, 14, 15, 16, 17});
                } else if (boss.getDir() == BossSprite.LEFT) {
                    boss.setDir(BossSprite.RIGHT);
                    boss.setFrameSequence(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8});
                }
            }
            if (boss.getX() < 0) {
                boss.setDir(EnemySprite.RIGHT);
            }
            if (boss.getX() > mapW) {
                boss.setDir(EnemySprite.LEFT);
            }
            if (mario.collidesWith(boss, true)) {
                marioAlive = false;
            }
        }
    }

    public void khaiBaoEnemy() {

//khai bao enemy nam
        EnemySprite nam1 = new EnemySprite(imgEnemyNam, 25, 25, 1, vtrEnemy, lm);
        nam1.setFrameSequence(new int[]{0, 1});
        nam1.setPosition(57, 251);
        nam1.setDir(EnemySprite.RIGHT);
        this.vtrEnemy.addElement(nam1);
        this.lm.insert(nam1, 0);

        EnemySprite nam2 = new EnemySprite(imgEnemyNam, 25, 25, 1, vtrEnemy, lm);
        nam2.setFrameSequence(new int[]{0, 1});
        nam2.setPosition(536, 251);
        nam2.setDir(EnemySprite.RIGHT);
        this.vtrEnemy.addElement(nam2);
        this.lm.insert(nam2, 0);

        EnemySprite nam3 = new EnemySprite(imgEnemyNam, 25, 25, 1, vtrEnemy, lm);
        nam3.setFrameSequence(new int[]{0, 1});
        nam3.setPosition(860, 251);
        nam3.setDir(EnemySprite.RIGHT);
        this.vtrEnemy.addElement(nam3);
        this.lm.insert(nam3, 0);

//khai bao enemy rua do
        EnemySprite ruaDo1 = new EnemySprite(imgEnemyRuaDo, 30, 30, 2, vtrEnemy, lm);
        ruaDo1.setFrameSequence(new int[]{2, 3});
        ruaDo1.setPosition(70, 243);
        ruaDo1.setDir(EnemySprite.RIGHT);
        this.vtrEnemy.addElement(ruaDo1);
        this.lm.insert(ruaDo1, 0);

        EnemySprite ruaDo2 = new EnemySprite(imgEnemyRuaDo, 30, 30, 2, vtrEnemy, lm);
        ruaDo2.setFrameSequence(new int[]{2, 3});
        ruaDo2.setPosition(800, 243);
        ruaDo2.setDir(EnemySprite.RIGHT);
        this.vtrEnemy.addElement(ruaDo2);
        this.lm.insert(ruaDo2, 0);

        EnemySprite ruaDo3 = new EnemySprite(imgEnemyRuaDo, 30, 30, 2, vtrEnemy, lm);
        ruaDo3.setFrameSequence(new int[]{2, 3});
        ruaDo3.setPosition(1280, 243);
        ruaDo3.setDir(EnemySprite.RIGHT);
        this.vtrEnemy.addElement(ruaDo3);
        this.lm.insert(ruaDo3, 0);

        EnemySprite ruaDo4 = new EnemySprite(imgEnemyRuaDo, 30, 30, 2, vtrEnemy, lm);
        ruaDo4.setFrameSequence(new int[]{2, 3});
        ruaDo4.setPosition(600, 243);
        ruaDo4.setDir(EnemySprite.RIGHT);
        this.vtrEnemy.addElement(ruaDo4);
        this.lm.insert(ruaDo4, 0);

//khai bao enemy rua sat
        EnemySprite ruaSat1 = new EnemySprite(imgEnemyRuaDo, 30, 30, 1, vtrEnemyRuaSat, lm);
        ruaSat1.setFrameSequence(new int[]{5, 6});
        ruaSat1.setPosition(90, 246);
        ruaSat1.setDir(EnemySprite.RIGHT);
        this.vtrEnemyRuaSat.addElement(ruaSat1);
        this.lm.insert(ruaSat1, 0);

        EnemySprite ruaSat2 = new EnemySprite(imgEnemyRuaDo, 30, 30, 1, vtrEnemyRuaSat, lm);
        ruaSat2.setFrameSequence(new int[]{5, 6});
        ruaSat2.setPosition(800, 246);
        ruaSat2.setDir(EnemySprite.RIGHT);
        this.vtrEnemyRuaSat.addElement(ruaSat2);
        this.lm.insert(ruaSat2, 0);

        EnemySprite ruaSat3 = new EnemySprite(imgEnemyRuaDo, 30, 30, 1, vtrEnemyRuaSat, lm);
        ruaSat3.setFrameSequence(new int[]{5, 6});
        ruaSat3.setPosition(1300, 246);
        ruaSat3.setDir(EnemySprite.RIGHT);
        this.vtrEnemyRuaSat.addElement(ruaSat3);
        this.lm.insert(ruaSat3, 0);

//khai bao enemy rua bay        
        EnemySprite ruaBay1 = new EnemySprite(imgEnemyRuaBay, 22, 30, 5, vtrEnemy, lm);
        ruaBay1.setFrameSequence(new int[]{0, 1});
        ruaBay1.setPosition(160, 125);
        ruaBay1.setDir(EnemySprite.LEFT);
        this.vtrEnemy.addElement(ruaBay1);
        this.lm.insert(ruaBay1, 0);

        EnemySprite ruaBay2 = new EnemySprite(imgEnemyRuaBay, 22, 30, 3, vtrEnemy, lm);
        ruaBay2.setFrameSequence(new int[]{2, 3});
        ruaBay2.setPosition(216, 210);
        ruaBay2.setDir(EnemySprite.RIGHT);
        this.vtrEnemy.addElement(ruaBay2);
        this.lm.insert(ruaBay2, 0);

        EnemySprite ruaBay3 = new EnemySprite(imgEnemyRuaBay, 22, 30, 3, vtrEnemy, lm);
        ruaBay3.setFrameSequence(new int[]{0, 1});
        ruaBay3.setPosition(761, 225);
        ruaBay3.setDir(EnemySprite.LEFT);
        this.vtrEnemy.addElement(ruaBay3);
        this.lm.insert(ruaBay3, 0);

        EnemySprite ruaBay4 = new EnemySprite(imgEnemyRuaBay, 22, 30, 3, vtrEnemy, lm);
        ruaBay4.setFrameSequence(new int[]{0, 1});
        ruaBay4.setPosition(970, 100);
        ruaBay4.setDir(EnemySprite.LEFT);
        this.vtrEnemy.addElement(ruaBay4);
        this.lm.insert(ruaBay4, 0);

        EnemySprite ruaBay5 = new EnemySprite(imgEnemyRuaBay, 22, 30, 3, vtrEnemy, lm);
        ruaBay5.setFrameSequence(new int[]{0, 1});
        ruaBay5.setPosition(1400, 210);
        ruaBay5.setDir(EnemySprite.LEFT);
        this.vtrEnemy.addElement(ruaBay5);
        this.lm.insert(ruaBay5, 0);

//khai bao enemy Cay
        EnemySprite cay1 = new EnemySprite(imgCay, 18, 36, 2, vtrEnemyCay, lm);
        cay1.setFrameSequence(new int[]{0, 1});
        cay1.setPosition(199, 190);
        cay1.setGocY(190);
        cay1.setDir(EnemySprite.DOWN);
        this.vtrEnemyCay.addElement(cay1);
        this.lm.insert(cay1, 25);

        EnemySprite cay2 = new EnemySprite(imgCay, 18, 36, 1, vtrEnemyCay, lm);
        cay2.setFrameSequence(new int[]{0, 1});
        cay2.setPosition(375, 190);
        cay2.setGocY(190);
        cay2.setDir(EnemySprite.DOWN);
        this.vtrEnemyCay.addElement(cay2);
        this.lm.insert(cay2, 25);

        EnemySprite cay3 = new EnemySprite(imgCay, 18, 36, 1, vtrEnemyCay, lm);
        cay3.setFrameSequence(new int[]{0, 1});
        cay3.setPosition(519, 125);
        cay3.setGocY(95);
        cay3.setDir(EnemySprite.DOWN);
        this.vtrEnemyCay.addElement(cay3);
        this.lm.insert(cay3, 25);

        EnemySprite cay4 = new EnemySprite(imgCay, 18, 36, 1, vtrEnemyCay, lm);
        cay4.setFrameSequence(new int[]{0, 1});
        cay4.setPosition(839, 125);
        cay4.setGocY(95);
        cay4.setDir(EnemySprite.DOWN);
        this.vtrEnemyCay.addElement(cay4);
        this.lm.insert(cay4, 25);

        EnemySprite cay5 = new EnemySprite(imgCay, 18, 36, 1, vtrEnemyCay, lm);
        cay5.setFrameSequence(new int[]{0, 1});
        cay5.setPosition(1271, 125);
        cay5.setGocY(127);
        cay5.setDir(EnemySprite.DOWN);
        this.vtrEnemyCay.addElement(cay5);
        this.lm.insert(cay5, 25);

        EnemySprite cay6 = new EnemySprite(imgCay, 18, 36, 1, vtrEnemyCay, lm);
        cay6.setFrameSequence(new int[]{0, 1});
        cay6.setPosition(1463, 161);
        cay6.setGocY(127);
        cay6.setDir(EnemySprite.DOWN);
        this.vtrEnemyCay.addElement(cay6);
        this.lm.insert(cay6, 25);

//khai bao enemy dan
        EnemySprite vienDan1 = new EnemySprite(imgTileset, 16, 16, 4, vtrEnemyDan, lm);
        vienDan1.setFrame(115);
        vienDan1.setPosition(1024, 208);
        vienDan1.setGocX(1024);
        vienDan1.setGocY(208);
        vienDan1.setDir(EnemySprite.LEFT);
        this.vtrEnemyDan.addElement(vienDan1);
        this.lm.insert(vienDan1, 0);

        EnemySprite vienDan2 = new EnemySprite(imgTileset, 16, 16, 4, vtrEnemyDan, lm);
        vienDan2.setFrame(115);
        vienDan2.setPosition(1087, 191);
        vienDan2.setGocX(1087);
        vienDan2.setGocY(191);
        vienDan2.setDir(EnemySprite.LEFT);
        this.vtrEnemyDan.addElement(vienDan2);
        this.lm.insert(vienDan2, 0);

        EnemySprite vienDan3 = new EnemySprite(imgTileset, 16, 16, 4, vtrEnemyDan, lm);
        vienDan3.setFrame(115);
        vienDan3.setPosition(1150, 175);
        vienDan3.setGocX(1150);
        vienDan3.setGocY(175);
        vienDan3.setDir(EnemySprite.LEFT);
        this.vtrEnemyDan.addElement(vienDan3);
        this.lm.insert(vienDan3, 0);

//khai bao enemy Boss
        BossSprite boss1 = new BossSprite(imgBoss, 57, 39, 2, 10);
        boss1.setFrameSequence(new int[]{0, 1, 2, 3, 4, 5});
        boss1.setPosition(1380, 232);
        boss1.setDir(EnemySprite.LEFT);
        this.vtrBoss.addElement(boss1);
        this.lm.insert(boss1, 0);
        bossHp = boss1.getBossHP();
    }

    public void khaiBaoEnemy2() {
//khai bao enemy mat xanh        
        EnemySprite matXanh1 = new EnemySprite(imgEnemyMatXanh, 24, 32, 3, vtrEnemyMatXanh, lm);
        matXanh1.setPosition(243, 32);
        matXanh1.setDir(EnemySprite.DOWN);
        matXanh1.setGocY(227);
        this.vtrEnemyMatXanh.addElement(matXanh1);
        this.lm.insert(matXanh1, 0);

        EnemySprite matXanh2 = new EnemySprite(imgEnemyMatXanh, 24, 32, 3, vtrEnemyMatXanh, lm);
        matXanh2.setPosition(405, 100);
        matXanh2.setDir(EnemySprite.DOWN);
        this.vtrEnemyMatXanh.addElement(matXanh2);
        this.lm.insert(matXanh2, 0);

        EnemySprite matXanh3 = new EnemySprite(imgEnemyMatXanh, 24, 32, 3, vtrEnemyMatXanh, lm);
        matXanh3.setPosition(692, 110);
        matXanh3.setDir(EnemySprite.DOWN);
        this.vtrEnemyMatXanh.addElement(matXanh3);
        this.lm.insert(matXanh3, 0);

        EnemySprite matXanh4 = new EnemySprite(imgEnemyMatXanh, 24, 32, 3, vtrEnemyMatXanh, lm);
        matXanh4.setPosition(788, 80);
        matXanh4.setDir(EnemySprite.DOWN);
        this.vtrEnemyMatXanh.addElement(matXanh4);
        this.lm.insert(matXanh4, 0);

        EnemySprite matXanh5 = new EnemySprite(imgEnemyMatXanh, 24, 32, 3, vtrEnemyMatXanh, lm);
        matXanh5.setPosition(884, 100);
        matXanh5.setDir(EnemySprite.DOWN);
        this.vtrEnemyMatXanh.addElement(matXanh5);
        this.lm.insert(matXanh5, 0);

//khai bao enemy rua ma        
        EnemySprite ruaMa1 = new EnemySprite(imgRuaMa, 30, 30, 1, vtrEnemyRuaMa, lm);
        ruaMa1.setFrameSequence(new int[]{5, 6});
        ruaMa1.setPosition(150, 227);
        ruaMa1.setDir(EnemySprite.RIGHT);
        this.vtrEnemyRuaSat.addElement(ruaMa1);
        this.lm.insert(ruaMa1, 0);

        EnemySprite ruaMa2 = new EnemySprite(imgRuaMa, 30, 30, 1, vtrEnemyRuaMa, lm);
        ruaMa2.setFrameSequence(new int[]{5, 6});
        ruaMa2.setPosition(640, 227);
        ruaMa2.setDir(EnemySprite.RIGHT);
        this.vtrEnemyRuaSat.addElement(ruaMa2);
        this.lm.insert(ruaMa2, 0);

//khai bao enemy lua ma
        EnemySprite luaMa1 = new EnemySprite(imgTileset, 16, 16, 6, vtrEnemyLuaMa, lm);
        luaMa1.setFrameSequence(new int[]{12, 13});
        luaMa1.setPosition(470, 240);
        luaMa1.setDir(EnemySprite.UP);
        this.vtrEnemyLuaMa.addElement(luaMa1);
        this.lm.insert(luaMa1, 0);

        EnemySprite luaMa2 = new EnemySprite(imgTileset, 16, 16, 6, vtrEnemyLuaMa, lm);
        luaMa2.setFrameSequence(new int[]{12, 13});
        luaMa2.setPosition(600, 250);
        luaMa2.setDir(EnemySprite.UP);
        this.vtrEnemyLuaMa.addElement(luaMa2);
        this.lm.insert(luaMa2, 0);

        EnemySprite luaMa3 = new EnemySprite(imgTileset, 16, 16, 6, vtrEnemyLuaMa, lm);
        luaMa3.setFrameSequence(new int[]{12, 13});
        luaMa3.setPosition(750, 270);
        luaMa3.setDir(EnemySprite.UP);
        this.vtrEnemyLuaMa.addElement(luaMa3);
        this.lm.insert(luaMa3, 0);

        EnemySprite luaMa4 = new EnemySprite(imgTileset, 16, 16, 6, vtrEnemyLuaMa, lm);
        luaMa4.setFrameSequence(new int[]{12, 13});
        luaMa4.setPosition(900, 290);
        luaMa4.setDir(EnemySprite.UP);
        this.vtrEnemyLuaMa.addElement(luaMa4);
        this.lm.insert(luaMa4, 0);

        EnemySprite luaMa5 = new EnemySprite(imgTileset, 16, 16, 6, vtrEnemyLuaMa, lm);
        luaMa5.setFrameSequence(new int[]{12, 13});
        luaMa5.setPosition(1050, 310);
        luaMa5.setDir(EnemySprite.UP);
        this.vtrEnemyLuaMa.addElement(luaMa5);
        this.lm.insert(luaMa5, 0);

        EnemySprite luaMa6 = new EnemySprite(imgTileset, 16, 16, 6, vtrEnemyLuaMa, lm);
        luaMa6.setFrameSequence(new int[]{12, 13});
        luaMa6.setPosition(1110, 330);
        luaMa6.setDir(EnemySprite.UP);
        this.vtrEnemyLuaMa.addElement(luaMa6);
        this.lm.insert(luaMa6, 0);

        EnemySprite luaMa7 = new EnemySprite(imgTileset, 16, 16, 6, vtrEnemyLuaMa, lm);
        luaMa7.setFrameSequence(new int[]{12, 13});
        luaMa7.setPosition(1176, 350);
        luaMa7.setDir(EnemySprite.UP);
        this.vtrEnemyLuaMa.addElement(luaMa7);
        this.lm.insert(luaMa7, 0);

//khai bao enemy boss 2
        BossSprite boss2 = new BossSprite(imgBoss2, 50, 50, 4, 10);
        boss2.setFrameSequence(new int[]{9, 10, 11, 12, 13, 14, 15, 16, 17});
        boss2.setPosition(1480, 206);
        boss2.setDir(EnemySprite.RIGHT);
        this.vtrBoss2.addElement(boss2);
        this.lm.insert(boss2, 0);
        bossHp = boss2.getBossHP();

//khai bao cong chua
        PrincessSprite congChua = new PrincessSprite(imgCongChua, 30, 38, 1);
        congChua.setFrameSequence(new int[]{0, 1, 2});
        congChua.setDir(PrincessSprite.RIGHT);
        congChua.setPosition(1356, 125);
        this.vtrCongChua.addElement(congChua);
        this.lm.insert(congChua, 0);

//khai bao gate
        gate = new Sprite(imgGate);
        gate.setPosition(1344, 96);
        this.lm.insert(gate, 0);

    }

    public void khaiBaoDauHoi() {
        BrickSprite br = new BrickSprite(imgTileset, 16, 16, 0);
        br.setFrameSequence(new int[]{105, 106, 107, 108});
        br.setPosition(444, 120);
        this.vtrDauHoi.addElement(br);

        BrickSprite br1 = new BrickSprite(imgTileset, 16, 16, 0);
        br1.setFrameSequence(new int[]{105, 106, 107, 108});
        br1.setPosition(126, 100);
        this.vtrDauHoi.addElement(br1);
        this.lm.insert(br1, 0);

        BrickSprite br2 = new BrickSprite(imgTileset, 16, 16, 0);
        br2.setFrameSequence(new int[]{105, 106, 107, 108});
        br2.setPosition(366, 150);
        this.vtrDauHoi.addElement(br2);
        this.lm.insert(br2, 0);

        BrickSprite br3 = new BrickSprite(imgTileset, 16, 16, 0);
        br3.setFrameSequence(new int[]{105, 106, 107, 108});
        br3.setPosition(519, 190);
        this.vtrDauHoi.addElement(br3);
        this.lm.insert(br3, 0);

        BrickSprite br5 = new BrickSprite(imgTileset, 16, 16, 0);
        br5.setFrameSequence(new int[]{105, 106, 107, 108});
        br5.setPosition(588, 120);
        this.vtrDauHoi.addElement(br5);

    }
// xu ly dau hoi

    public void dauHoiProcess() {
        for (int i = 0; i < vtrDauHoi.size(); i++) {
            BrickSprite dh = (BrickSprite) vtrDauHoi.elementAt(i);
            dh.nextFrame();
            if (mario.collidesWith(dh, true) && hdir.equals("u")) {
                this.playSound("/com/vtc/sound/CollectCoin.wav");
                hdir = "d";
                dh.setFrameSequence(new int[]{109});
                lm.insert(dh, 0);
                Sprite banh = new Sprite(imgBanh, 16, 32);
                banh.setPosition(dh.getX(), dh.getY() - 32);
                this.vtrBanh.addElement(banh);
                lm.insert(banh, 0);
            }
            if (mario.collidesWith(dh, true)) {
                if (this.vdir.equals("d")) {
                    mario.setPosition(mario.getX(), mario.getY() - fallDown);
                    jump = 0;
                }
            }
            if (mario.collidesWith(dh, true)) {
                if (this.hdir.equals("d")) {
                    mario.setPosition(mario.getX(), mario.getY() + fallDown);
                    jump = 0;
                }
            }
        }
    }
// xu ly vu no

    public void explosionProcess() {
        for (int i = 0; i < vtrExplosion.size(); i++) {
            Sprite f = (Sprite) vtrExplosion.elementAt(i);
            f.nextFrame();
            if (f.getFrame() >= f.getRawFrameCount() - 1) {
                this.vtrExplosion.removeElement(f);
                this.lm.remove(f);
            }
        }
    }

    // xu ly banh
    public void banhProcess() {
        for (int i = 0; i < vtrBanh.size(); i++) {
            Sprite b = (Sprite) vtrBanh.elementAt(i);
            b.nextFrame();
            if (b.getFrame() >= b.getRawFrameCount() - 1) {
                this.vtrBanh.removeElement(b);
                this.lm.remove(b);
            }
        }
    }

    public void loadGame() throws IOException {

        this.playSound("/com/vtc/sound/smb_stage_clear.wav");

        imgMario = Image.createImage("/com/vtc/image/mario/abc.png");
        imgTileset = Image.createImage("/com/vtc/image/mario/tileset1.png");
        imgBullet = Image.createImage("/com/vtc/image/mario/fire.png");
        imgExplosion = Image.createImage("/com/vtc/image/mario/explosion.png");
        imgEnemyNam = Image.createImage("/com/vtc/image/mario/nam.png");
        imgEnemyRuaDo = Image.createImage("/com/vtc/image/mario/rua_do.png");
        imgEnemyRuaBay = Image.createImage("/com/vtc/image/mario/rua_bay.png");
        imgEnemyMatXanh = Image.createImage("/com/vtc/image/mario/mat_xanh.png");
        imgBoss = Image.createImage("/com/vtc/image/mario/bosses1.png");
        imgBoss2 = Image.createImage("/com/vtc/image/mario/bosses2.png");
        imgBanh = Image.createImage("/com/vtc/image/mario/banh.png");
        imgCay = Image.createImage("/com/vtc/image/mario/cay.png");
        imgRuaMa = Image.createImage("/com/vtc/image/mario/rua_ma.png");
        imgCongChua = Image.createImage("/com/vtc/image/mario/cong_chua.png");
        imgGate = Image.createImage("/com/vtc/image/mario/gate.png");
        imgWin = Image.createImage("/com/vtc/image/mario/win.png");

        mario = new MarioSpite(imgMario, 21, 35, 3);
        mario.setFrame(0);
        mario.defineReferencePixel(13, 13);

        bullet = new BulletSprite(imgBullet, 10, 9, 2);
        bullet.setFrame(0);

        this.lm = new LayerManager();

        if (level == 1) {
            this.road = this.drawMap("/com/vtc/map/smd/road.smp", imgTileset);
            this.backGround = this.drawMap("/com/vtc/map/smd/BackGround.smp", imgTileset);
            this.layout = this.drawMap("/com/vtc/map/smd/Layout.smp", imgTileset);
            this.gameOver = this.drawMap("/com/vtc/map/smd/GameOver.smp", imgTileset);

            mario.setPosition(12, 235);

            this.lm.append(mario);
            this.lm.append(road);
            this.lm.append(backGround);
            this.lm.append(gameOver);
            this.lm.append(layout);

            this.khaiBaoBrick();
            this.khaiBaoEnemy();
            this.khaiBaoDauHoi();

        } else if (level == 2) {

            this.road = this.drawMap("/com/vtc/map/smd2/road.smp", imgTileset);
            this.layout = this.drawMap("/com/vtc/map/smd2/Layout.smp", imgTileset);
            this.gameOver = this.drawMap("/com/vtc/map/smd2/GameOver.smp", imgTileset);
            
            mario.setPosition(12, 135);

            this.lm.append(mario);
            this.lm.append(road);
            this.lm.append(gameOver);
            this.lm.append(layout);

            this.khaiBaoEnemy2();
        }


        this.mapW = road.getWidth();
        this.mapH = road.getHeight();
    }

    public void startGame() {
        try {
            this.loadGame();
            super.startGame();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void viewportProcess() {
        int centerX = mario.getX() + mario.getWidth() / 2;
        int centerY = mario.getY() + mario.getHeight() / 2;

        int viewX = centerX - this.w / 2;
        int viewY = centerY - this.h * 3 / 4;

        if (viewX < 0) {
            viewX = 0;
        } else if (viewX + w > mapW) {
            viewX = mapW - w;
        }
        if (viewY < 0) {
            viewY = 0;
        } else if (viewY + h > mapH) {
            viewY = mapH - h;
        }

        this.lm.setViewWindow(viewX, viewY, w, h);
    }
}
